﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DAL.SQLStatements;
using BAL.Entities;
using BAL.Utils;
using System.Data;


namespace DAL.LogicComponents
{
    public class clsEmployeeDAL
    {
        #region -- Login Admin
        public static bool queryLoginAdmin(clsEmployee obj)
        {
            clsQueryBuilder query = new clsQueryBuilder();
            query.propTable = "tblEmployee";
            query.propColumn = "colEmployeeID";
            query.propWhere = "colUserName ='" + obj.UserName + "' and colPassword='"+obj.Password+"'";
            
            return query.getBoolValue();
        }
        #endregion

        #region --- Get All Employee
        public static DataTable queryGetEmployeeList()
        {
            // Select all of products by user id
            clsQueryBuilder query = new clsQueryBuilder();
            query.propTable = "tblEmployee";
            query.propColumn = "colEmployeeID,colUserName,colEmail,colCellPhone,colAddress,colHomePhone";
            query.propWhere = "colEmployeeID > 0";

            // return result
            return query.getDatatable();
        }
        #endregion ---

        #region --- Get All Employee by id
        public static DataTable queryGetEmployeeListById(clsEmployee obj)
        {
            // Select all of products by user id
            clsQueryBuilder query = new clsQueryBuilder();
            query.propTable = "tblEmployee";
            query.propColumn = "*";
            query.propWhere = "colEmployeeID = '" + obj.EmployeeID + "'";

            // return result
            return query.getDatatable();
        }
        #endregion ---

        #region --- Add new Employee
        public static bool queryAddEmployee(clsEmployee obj)
        {
            // Add value to column(s)
            Dictionary<String, Object> data = new Dictionary<String, Object>();
            data.Add("colUserName", "N'" + obj.UserName + "'");
            data.Add("colPassword", "'" + obj.Password + "'");
            data.Add("colEmail", "'" + obj.Email + "'");
            data.Add("colAvatar", "'" + obj.Avatar + "'");
            data.Add("colCellPhone","0" + obj.PhoneNumber);
            data.Add("colAddress", "N'" + obj.Address + "'");
            data.Add("colHomePhone", "0" + obj.HomePhone);
            data.Add("colInsertDate", "'" + clsDateTimeSetting.UTCTime() + "'");


            //Execute insert query
            clsQueryBuilder query = new clsQueryBuilder();
            return query.executeInsertInto("tblEmployee", data);
        }
        #endregion

        #region --- Update Employee
        public static bool queryUpdateEmployee(clsEmployee obj)
        {
            Dictionary<String, Object> cols = new Dictionary<String, Object>();
            cols.Add("colEmail", "'" + obj.Email + "'");
            if(obj.Avatar != ""){
                cols.Add("colAvatar", "'" + obj.Avatar + "'");
            }
            cols.Add("colCellPhone","0"+ obj.PhoneNumber);
            cols.Add("colAddress", "N'" + obj.Address + "'");
            cols.Add("colHomePhone", "0" + obj.HomePhone);
            cols.Add("colLastUpdate", "'" + clsDateTimeSetting.UTCTime() + "'");
            // Execute update query
            clsQueryBuilder query = new clsQueryBuilder();
            return query.executeUpdate("tblEmployee", cols, String.Format("colEmployeeID = {0}", obj.EmployeeID));

        }
        #endregion

        #region -- check Employee exist
        public static bool CheckEmployeeExist(clsEmployee obj)
        {
            clsQueryBuilder query = new clsQueryBuilder();
            query.propColumn = "colUserName";
            query.propTable = "tblEmployee";
            query.propWhere = "colEmployeeID > 0 and colUserName = N'" + obj.UserName + "'";

            return query.getBoolValue();

        }
        #endregion
    }
}